package com.wdcloud.niceandroid.netutil;


import com.wdcloud.framework_business.utils.LogUtil;
import com.wdcloud.netlibrary.callback.NetWorkCallback;
import com.wdcloud.netlibrary.exception.NetWorkException;
import com.wdcloud.niceandroid.utils.AppUtil;
import com.wdcloud.niceandroid.utils.GsonUtil;
import com.wdcloud.niceandroid.utils.ToastUtil;

import org.apache.http.conn.ConnectTimeoutException;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;


public abstract class HttpCallback<T> extends NetWorkCallback<T> {

    @Override
    public void onSuccess(T t) {
        super.onSuccess(t);
        if (t == null) {
            // 异常
            onFail(new NetsException(NetsCode.STATUS_JSON_NO, "解析出错"));
            return;
        }
        if (t instanceof BaseBean) {
            if (((BaseBean) t).getCode() == NetsCode.TOKEN_LOSE) {
                LogUtil.d("=====================请先登录====================");
                AppUtil.logOutSuccess();
                return;
            }
        }
        /**日志打印*/
        LogUtil.json(GsonUtil.getGsonInstance().toJson(t));
        handleSuccess(t);

//        if (!t.isSuccess()) {
//            // 异常
//            if (TextUtils.isEmpty(t.getTips())) {
//                t.setTips("服务器异常，请稍后重试");
//            }
//            onFail(new NetsException(t.getCode(), t.getTips()));
//        } else {
//            handleSuccess(t);
//        }
    }

    @Override
    public void onFail(Exception e) {
        super.onFail(e);
        e.printStackTrace();
        handleException(parseException(e));
//        if (e instanceof NetsException) {
//            loginLose((NetsException) e);
//        } else {
//            handleException(parseException(e));
//        }
    }

    public static Exception parseException(Exception e) {
        Exception handledException;
        if (e instanceof SocketTimeoutException) {
            // 超时
            handledException = new NetsException(NetsCode.STATUS_TIMEOUT, "网络连接超时");
        } else if (e instanceof ConnectTimeoutException) {
            // 超时
            handledException = new NetsException(NetsCode.STATUS_TIMEOUT, "网络连接超时");
        } else if (e instanceof UnknownHostException || e instanceof ConnectException) {
            String netError = "网络状态异常";
            // 无网络
            ToastUtil.showToast(netError);
            handledException = new NetsException(NetsCode.STATUS_NO_NET, netError);
        } else if (e instanceof NetWorkException) {
            // 服务器异常
            handledException = new NetsException(NetsCode.STATUS_SERVER_ERROR, "服务器异常");
        } else if (e instanceof SocketException) {
            // 您的网络已断开
            handledException = new NetsException(NetsCode.STATUS_SERVER_ERROR, "您的网络已断开");
        } else {
            // 其他异常
            handledException = e;
        }
        return handledException;
    }

    private void loginLose(NetsException e) {
//        int code = e.getCode();
//        if (code == NetsCode.TOKEN_LOSE || code == NetsCode.TOKEN_CAN_NOT_RESOLVE) {
//            EventDispatcher.getInstance()
//                    .publish(new Event(EventConstants.EVENT_FORCE_LOGOUT, null));
//        } else {
//            handleException(parseException(e));
//        }
    }

    public abstract void handleException(Exception e);

    public abstract void handleSuccess(T baseModel);

//    public abstract void handleTokenLose();
}

